from datetime import datetime
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./db/pms.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)


def model_to_json_test():
    from flask import jsonify
    q = db.session.query(WhiteSite).first()  # db = SQLAlchemy()
    q_dict = serialize(q)
    q_json = jsonify(q_dict)
    return q_json


def serialize(model):
    from sqlalchemy.orm import class_mapper
    columns = [c.key for c in class_mapper(model.__class__).columns]
    return dict((c, getattr(model, c)) for c in columns)  # 字典生成器 dict((c, ord(c)) for c in 'abcd')


class WhiteSite(db.Model):
    __tablename__ = 'WHITE_SITE'
    site_name = db.Column(db.String(128), primary_key=True)
    comments = db.Column(db.String(1000))
    create_time = db.Column(db.DateTime, default=func.now())
    create_user = db.Column(db.String(128))
    last_modified_time = db.Column(db.DateTime, default=func.now(), onupdate=func.now())
    last_modified_user = db.Column(db.String(512))
